<template>
  <div>
    <div class="search">
      <div class="search_top">
        <span class="iconfont icon-xiangzuo" @click="gobefore"></span>
        <input
          type="text"
          placeholder="搜索歌曲、歌手"
          @keyup.13="enter"
          :value="value"
        />
        <!-- v-model="value" -->
        <span class="iconfont icon-shanchu" @click="dels"></span>
      </div>
      <div class="search_content" v-if="show">
        <p>热门搜索</p>
        <div class="hotSinger">
          <ul>
            <li
              v-for="(item, index) in list"
              :key="index"
              @click="select(item.name)"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
        <div class="histRecord">
          <p>
            <span>搜索历史</span>
            <span class="iconfont icon-shanchu1" @click="clearAll"></span>
          </p>
          <ul>
            <li v-for="(item, index) in history" :key="index">
              <span>{{ item }}</span>
              <span class="iconfont icon-shanchu" @click="del(index)"></span>
            </li>
          </ul>
        </div>
      </div>
      <div ref="contents" class="bigbox" v-if="!show">
        <div>
          <div class="viewsinger">最佳匹配</div>
          <div class="sigerImg">
            <img :src="searchList[0].al.picUrl" alt="" />
            <div>
              歌手：<span v-for="(k, i) in searchList[0].ar" :key="i">{{
                k.name
              }}</span>
            </div>
          </div>
          <!-- <div class="sigerImg">
            <img src="" alt="" />
            <div>歌单：<span></span></div>
          </div> -->
          <div
            class="everysong"
            v-for="(item, index) in searchList"
            :key="index"
            @click="playSong(item.id)"
          >
            <div class="songMsg">
              <h2>{{ item.name }}</h2>
              <h3>
                <span v-for="(k, i) in item.ar" :key="i">
                  {{ k.name }} &nbsp;&nbsp;
                </span>
              </h3>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "@better-scroll/core";
import { getTopArtists, getCloudSearch } from "../api/search.js";
export default {
  data() {
    return {
      show: true,
      value: null,
      list: ["薛之谦"],
      history: [],
      searchList: [
        {
          al: {
            picUrl: null,
          },
        },
      ],
    };
  },
  methods: {
    // 滚动效果初始化方法
    _initScroll(dom, flagx, flagy) {
      // 右侧滚动效果初始化
      // this.rights = new BScroll(this.$refs.right, {
      this.rights = new BScroll(dom, {
        click: true, //点击触发
        probeType: 3, //深针的效果，时时获取滚动高度
        scrollX: flagx,
        scrollY: flagy,
      });
      // console.log(this.$refs.right);
    },
    // /top/artists?offset=0&limit=30
    getTopArtistsFn() {
      getTopArtists({ limit: 10 }).then((data) => {
        // console.log(data);
        this.list = data.artists;
      });
    },
    // /cloudsearch?keywords= 海阔天空
    getCloudSearchFn(values) {
      getCloudSearch({ keywords: values }).then((data) => {
        this.searchList = data.result.songs;
        // console.log(this.searchList);
      });
    },
    gobefore() {
      this.$router.go(-1);
    },
    select(item) {
      this.value = item;
    },
    enter(event) {
      this.value = event.target.value;
      this.history.push(this.value);
      this.show = false;
      this.getCloudSearchFn(this.value);
      this.value = "";
    },
    clearAll() {
      this.history = [];
    },
    del(index) {
      this.history.splice(index, 1);
    },
    dels() {
      this.value = "";
    },
    playSong(id) {
      this.$emit("playSong", id);
    },
  },
  created() {
    this.getTopArtistsFn();
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this._initScroll(this.$refs.contents, false, true);
      }, 3000);
    });
  },
};
</script>

<style lang="less" scoped>
.search {
  .search_top {
    height: 50px;
    line-height: 50px;
    background-color: #d4473c;
    display: flex;
    align-items: center;

    span {
      width: 50px;
      height: 50px;
      font-size: 20px;
      color: #fff;
      text-align: center;
    }
    input {
      flex: 1;
      height: 30px;
      outline: none;
      background-color: transparent;
      border: none;
      border-bottom: 1px solid #eee;
      color: #eee;
      &::placeholder {
        color: #eee;
      }
    }
  }
  .search_content {
    background-color: #fff;
    margin: 16px;
    p {
      height: 40px;
      line-height: 40px;
      color: #a9aaaa;
    }
    .hotSinger {
      ul {
        display: flex;
        flex-wrap: wrap;
        font-size: 18px;
        color: #676869;
        li {
          border: 1px solid #dbdcdd;
          width: auto;
          padding: 4px;
          border-radius: 6px;
          margin-right: 10px;
          margin-bottom: 10px;
        }
      }
    }
    .histRecord {
      p {
        height: 40px;
        line-height: 40px;
        color: #a9aaaa;
        display: flex;
        justify-content: space-between;
        span:last-child {
          color: #555;
        }
      }
      ul {
        li {
          display: flex;
          justify-content: space-between;
          color: #676869;

          span {
            font-size: 18px;
            &:last-child {
              font-weight: 700;
              color: #555;
            }
          }
        }
      }
    }
  }
}
.bigbox {
  min-width: 320px;
  max-width: 750px;
  margin: 0 auto;
  width: 100%;
  position: absolute;
  top: 50px;
  bottom: 0px;
  // 这是为了滚动时隐藏溢出
  overflow: hidden;
  .viewsinger {
    width: 100%;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    margin: 10px;
    color: #d4473c;
    user-select: none;
  }
  .sigerImg {
    width: 100%;
    height: 80px;
    margin: 0 10px;
    padding: 10px 0;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f2f2f3;
    img {
      width: 80px;
      height: 80px;
      background-color: pink;
    }
    div {
      font-size: 18px;
      margin-left: 20px;
      span {
        color: #507daf;
      }
    }
  }
  .everysong {
    width: 100%;

    color: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid hsl(240, 4%, 95%);
    margin: 0 10px;
    padding: 10px 0;
    user-select: none;
    .songMsg {
      font-size: 17px;

      h2 {
        font-size: 16px;
      }
      h3 {
        font-size: 12px;
        color: #888;
      }
    }
  }
}
</style>